﻿using System;
using ITViet.Domains;
using ITViet.Services;
using ITViet.Web.App_Code;

namespace ITViet.Web.Admin
{
  public partial class SettingPage : AdminBasePage
  {
    internal override int RequiredPermission
    {
      get
      {
        return (int)FunctionList.SystemSetting;
      }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadSetting();
      }
    }

    protected void ctrlCallback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
      string[] para = e.Parameter.Split('|');
      if (para[0] == "Save")
      {
        e.Result = SaveSetting(para[1]);
      }
    }

    private void LoadSetting()
    {
      txtHost.Text = GlobalCache.Instance.SMTPHost;
      txtPort.Text = GlobalCache.Instance.SMTPPort.ToString();
      ckbSSL.Checked = GlobalCache.Instance.SMTPSSL;
      txtUser.Text = GlobalCache.Instance.SMTPUser;
      txtPass.Text = GlobalCache.Instance.SMTPPass;
      txtContactEmail.Text = GlobalCache.Instance.SMTPReceiver;
      txtNotifyBookingEmail.Text = GlobalCache.Instance.BookingMail;

      txtRateEx.Text = GlobalCache.Instance.Link_RateExchange;
      txtGold.Text = GlobalCache.Instance.Link_Gold;
      txtStock.Text = GlobalCache.Instance.Link_Stock;
      txtTV.Text = GlobalCache.Instance.Link_TV;
      txtLoto.Text = GlobalCache.Instance.Link_Loto;

    }

    private string SaveSetting(string type)
    {
      try
      {
        SettingService service = new SettingService();
        TList<Domains.Setting> sList = new TList<Setting>();
        switch (type)
        {
          case "MAIL":
            string enableSSL = (ckbSSL.Checked) ? "true" : "false";
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.SMTP_Host, txtHost.Text));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.SMTP_Port, txtPort.Text));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.SMTP_SSL, enableSSL));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.SMTP_User, txtUser.Text));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.SMTP_Pass, txtPass.Text));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.MAIL_BOOKING, txtNotifyBookingEmail.Text));
            break;
          case "LINK":
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.RATE_EXCHANGE, Utils.FormatWebLink(txtRateEx.Text)));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.GOLD, Utils.FormatWebLink(txtGold.Text)));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.ONLINE_STOCK, Utils.FormatWebLink(txtStock.Text)));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.TV_ONLINE, Utils.FormatWebLink(txtTV.Text)));
            sList.Add(GlobalCache.Instance.SetSettingById((int)SettingList.LOTO, Utils.FormatWebLink(txtLoto.Text)));
            break;
          default:
            break;
        }
        service.Save(sList);
        GlobalCache.Instance.LoadDBSetting();
      }
      catch (Exception ex)
      {
        WriteLog("Services.aspx::Save Info", ex);
        return Resources.Messages.ERR0004;
      }
      return "OK|" + type;
    }
  }
}
